import RedisWrapper from '../redis/RedisWrapper';
import { UserModel } from '../model/UserModel';
import { isNull } from 'lodash-es';

class UserService {
  async getCacheUser(userId: number): Promise<UserModel | null> {
    const key = `'ts_node:user_info_key:${userId}`;
    let user: UserModel | null = await RedisWrapper.get<UserModel>(key);
    if (!user) {
      user = await UserModel.findByPk(userId);
      if (!isNull(user)) {
        await RedisWrapper.set(key, user);
      }
    }
    return user;
  }
}

export default new UserService();
